package com.fuhailiu.opengl.activities;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.fuhailiu.opengl.define.AppGlobalDef;
import com.fuhailiu.opengl.utils.DensityUtil;
import com.fuhailiu.opengl.utils.LogUtil;
import com.fuhailiu.opengl.view.MGLSurfaceView;

public class OpenGLActivity extends Activity {

	private final String TAG = getClass().getSimpleName();

	private MGLSurfaceView mGLSurfaceView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		LogUtil.LogD(TAG, "onCreate   <---");

		super.onCreate(savedInstanceState);

		int renderId = getIntent().getIntExtra(AppGlobalDef.INTENT_KEY_RENDER, AppGlobalDef.RenderIds.RENDER_TRIANGLE);
		mGLSurfaceView = new MGLSurfaceView(this, renderId);
		setContentView(mGLSurfaceView);

		initScreenSize();

		LogUtil.LogD(TAG, "onCreate   --->");
	}

	private void initScreenSize() {
		AppGlobalDef.SCREEN_SIZE = DensityUtil.getScreenSize(this);
	}

	@Override
	protected void onResume() {
		LogUtil.LogD(TAG, "onResume   <---");

		super.onResume();
		mGLSurfaceView.onResume();

		LogUtil.LogD(TAG, "onResume   --->");
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		LogUtil.LogD(TAG, "onCreateOptionsMenu   <---");

		String[] menuList = mGLSurfaceView.getRender().getMenuInfo();
		if (null != menuList && menuList.length > 0) {
			for (String title : menuList)
				menu.add(title);
		}

		LogUtil.LogD(TAG, "onCreateOptionsMenu   --->");

		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		LogUtil.LogD(TAG, "onOptionsItemSelected   <---");

		String title = item.getTitle().toString();
		mGLSurfaceView.getRender().onMenuSelected(title);

		LogUtil.LogD(TAG, "onOptionsItemSelected   --->");

		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onPause() {
		LogUtil.LogD(TAG, "onPause   <---");

		super.onPause();
		mGLSurfaceView.onPause();

		LogUtil.LogD(TAG, "onPause   --->");
	}

	@Override
	protected void onDestroy() {
		LogUtil.LogD(TAG, "onDestroy   <---");
		super.onDestroy();
		LogUtil.LogD(TAG, "onDestroy   --->");
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		LogUtil.LogD(TAG, "onConfigurationChanged   <---");
		super.onConfigurationChanged(newConfig);
		initScreenSize();
		LogUtil.LogD(TAG, "onConfigurationChanged   --->");
	}

}
